package cyf.Owner.service;

import cyf.Owner.bean.CarVo;
import cyf.Owner.bean.OwnerVo;
import cyf.Owner.dao.OwnerDao;
import cyf.Tools.PAGE_MESSAGE;
import cyf.Tools.PageObject;
import cyf.Tools.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OwnerService {
    @Autowired
    private OwnerDao ownerDao;

    private static Integer PAGE_SIZE =  PAGE_MESSAGE.PAGE_SIZE;

    /**
     * 查询业主列表
     * @param param
     * @return
     */
    public Map<String,Object> getAllOwner(Map<String,Object> param, Integer pageCurrent) throws Exception {
        //1.验证参数的有效性
        if(pageCurrent==null||pageCurrent<1)
            pageCurrent = 1;
        //2.获取当前页数据
        //2.1 计算startIndex的值
        Integer pageSize=PAGE_SIZE;
        Integer startIndex=(pageCurrent-1)*pageSize;
        //3.获取总记录数(根据此值计算总页数)
        Integer rowCount= ownerDao.getRowCount();
        PageObject pageObject=new PageObject();
        pageObject.setRowCount(rowCount);
        pageObject.setPageSize(pageSize);
        pageObject.setPageCurrent(pageCurrent);
        pageObject.setStartIndex(startIndex);//可选
        pageObject.setPageCount(pageObject.getPageCount());
        param.put("startIndex",startIndex);
        param.put("pageSize",pageSize);
        List<OwnerVo> result =  ownerDao.getAllOwner(param);
        Map<String,Object> map = new HashMap<>();
        ServerResponse<List<OwnerVo>> serverResponse = ServerResponse.createServerRespsonse(1,"查询到列表",result);
        map.put("serverResponse",serverResponse);
        map.put("pageObject",pageObject);
        map.put("pagesize",PAGE_SIZE);
        return map;
    }

    /**新增业主**/
    public Map<String,Object> insertOwner(OwnerVo owner){
        Map<String,Object> param = new HashMap<>();
        Map<String,Object> result = new HashMap<>();
        result.put("error",0);
        if("".equals(owner.getOwner_address()) || owner.getOwner_address() == null){
            result.put("error","地址为空！");
            return result;
        }
        if("".equals(owner.getOwner_name()) || owner.getOwner_name() == null){
            result.put("error","姓名为空！");
            return result;
        }
        if("".equals(owner.getOwner_phone()) || owner.getOwner_phone() == null){
            result.put("error","电话为空！");
            return result;
        }
        if("".equals(owner.getOwner_zjhm()) || owner.getOwner_zjhm() == null){
            result.put("error","身份证号码为空！");
            return result;
        }
        if("".equals(owner.getOwner_sex()) || owner.getOwner_sex() == null){
            result.put("error","性别为空！");
            return result;
        }

        param.put("owner_address",owner.getOwner_address());
        param.put("owner_phone",owner.getOwner_phone());
        param.put("owner_sex",owner.getOwner_sex());
        param.put("owner_zjhm",owner.getOwner_zjhm());

        param.put("startIndex",0);
        param.put("pageSize",1);
        //是否有身份证一样的业主
        List<OwnerVo> owners = ownerDao.getAllOwner(param);
        result.put("repeat",0);
        if(owners.size()>0){
            result.put("repeat",1);
            return result;
        }
        param.put("owner_name",owner.getOwner_name());
        boolean isSuccess = ownerDao.insertOwner(param);
        result.put("success",isSuccess);

        return result;
    }

    /**
     * 新增一条业主车辆记录
     */
    public Map<String,Object> insertCar(CarVo car){
        //先查找是否有该业主
        String owner_id = car.getCar_owner_id();
        Map<String,Object> param1 = new HashMap<>();
        param1.put("owner_zjhm",owner_id);
        param1.put("startIndex",0);
        param1.put("pageSize",1);
        List<OwnerVo> ex = ownerDao.getAllOwner(param1);
        //再查找是否有这个车牌号的车
        Map<String,Object> param3 = new HashMap<>();
        param3.put("car_plate",car.getCar_plate());
        param3.put("startIndex",0);
        param3.put("pageSize",1);
        List<CarVo> cars = ownerDao.getCars(param3);
        Map<String,Object> result = new HashMap<>();
        if(ex.size()>0){ //该业主可用
            if(cars.size()>0){
                result.put("plrepeat",1);
                return result;
            }
            Map<String,Object> param2 = new HashMap<>();
            param2.put("car_owner_id",car.getCar_owner_id());
            param2.put("car_type",car.getCar_type());
            param2.put("car_plate",car.getCar_plate());
            boolean isSuccess = ownerDao.insertCar(param2);
            result.put("success",isSuccess);
            return result;
        }else{ //不可用 新增失败
            result.put("success",false);
            result.put("repeat",0);
            return result;
        }
    }

    /**
     * 查询车辆列表
     * @param param
     * @return
     */
    public Map<String,Object> getCarsByOwner(Map<String,Object> param,Integer pageCurrent) throws Exception {
        //1.验证参数的有效性
        if(pageCurrent==null||pageCurrent<1)
            pageCurrent = 1;
        //2.获取当前页数据
        //2.1 计算startIndex的值
        Integer pageSize=PAGE_SIZE;
        Integer startIndex=(pageCurrent-1)*pageSize;
        //3.获取总记录数(根据此值计算总页数)
        Integer rowCount= ownerDao.getCarsRowCount(param);
        PageObject pageObject=new PageObject();
        pageObject.setRowCount(rowCount);
        pageObject.setPageSize(pageSize);
        pageObject.setPageCurrent(pageCurrent);
        pageObject.setStartIndex(startIndex);//可选
        pageObject.setPageCount(pageObject.getPageCount());
        param.put("startIndex",startIndex);
        param.put("pageSize",pageSize);
        List<CarVo> result =  ownerDao.getCarsByOwner(param);
        Map<String,Object> map = new HashMap<>();
        map.put("count",result.size());
        map.put("result",result);
        map.put("pageObject",pageObject);
        map.put("pagesize",PAGE_SIZE);
        return map;
    }

    /**
     * 根据车牌号删除一个车辆
     * @param param
     * @return
     */
    public Map<String,Object> deleteCarByPlate(Map<String,Object> param){
        boolean deletesuccess = this.ownerDao.deleteCayByPlate(param);
        Map<String,Object> result = new HashMap<>();
        result.put("success",deletesuccess);
        if(deletesuccess){
            result.put("mes","删除成功！");
        }else{
            result.put("mes","删除失败！");
        }
        return result;
    }

    /**
     * 删除一个业主并删除所属车辆
     * @param param
     * @return
     */
    public  Map<String,Object> deleteOwner(Map<String,Object> param){
        boolean deletesuccess1 = this.ownerDao.deleteOwnerBySfz(param);
        boolean deletesuccess2 = this.ownerDao.deleteCarByOwner(param);
        Map<String,Object> result = new HashMap<>();
        result.put("success",deletesuccess1);
        if(deletesuccess1){
            result.put("mes","删除成功！");
        }else{
            result.put("mes","删除失败！");
        }
        return result;
    }
}
